package com.chuck.controller;

import com.chuck.entity.SysMenu;
import com.chuck.service.SysMenuService;
import com.chuck.utils.DataGridResult;
import com.chuck.utils.Query;
import com.chuck.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/sys/menu")
public class SysMenuController {

    @Autowired
    private SysMenuService sysMenuService;

    @GetMapping("/listall")
    public List<SysMenu> getAll(){
        return sysMenuService.queryListAll();
    }

    @GetMapping("/list")
    public DataGridResult getPageList(@RequestParam Map<String,Object> params){

        Query query= new Query(params);
        DataGridResult r = sysMenuService.getPageList(query);
        return r;
    }
    /*第五步：*/
    @PostMapping("/del")
    public R deleteBatch(@RequestBody Long[] menuIds){
        for (Long menuId: menuIds){
            if (menuId <=31){
                return R.error("系统菜单，不能删除！！");
            }
        }
        sysMenuService.deleteBatch(menuIds);

        return R.ok("删除成功！！");
    }

    @GetMapping("/select")
    public R select(){
        List<SysMenu> sysMenus = sysMenuService.queryNoButtonList();
        SysMenu root = new SysMenu();//创建一个根节点
        root.setName("一级菜单");
        root.setMenuId(0L);
        root.setParentId(-1L);
        root.setOpen(true);

        sysMenus.add(root);
        return  R.ok().put("menuList",sysMenus);
    }
    /*新增菜单接口*/
    @PostMapping("/save")
    public R save(@RequestBody SysMenu menu){
        sysMenuService.save(menu);
        return R.ok("保存成功");
    }

    @GetMapping("/info/{menuId}")
    public R info(@PathVariable("menuId") Long menuId){
        SysMenu sysMenu = sysMenuService.queryObject(menuId);
        return R.ok("修改成功").put("menu",sysMenu);
    }

    /*菜单修改接口*/
    @PostMapping("/update")
    public R update(@RequestBody SysMenu menu){
        sysMenuService.update(menu);
        return R.ok("修改成功");
    }

}
